/////precompiled////////
#include "precompiled.h"
////////////////////////


#include "frm_control_TImage.h"

using namespace frm;
using namespace control;

// TImage /////////////////////////////////////////////////////////////////////

//
TImage::TImage(TWindow &Window) : TPanel(Window)
{
  //Rect.X.SetCalcSize(ECalcSize_Fixed);
  //Rect.Y.SetCalcSize(ECalcSize_Fixed);
  value = NULL;
}

//
void TImage::SetValue(grf::TImage *V)
{
  value = V;

  if (value) {
    SetClientWidth (value->Width());
    SetClientHeight(value->Height());
  }
  else {
    SetClientWidth (30);
    SetClientHeight(30);
  }

  //Rect.Y.SetBase(V.Height());
  //Rect.X.SetSize(V.Width ());
  //Rect.Y.SetSize(V.Height());

  //Rect.SetSize(V.Width(),V.Height());
  //Rect.Y.SetBase(V.Height());
}

//
grf::TImage *TImage::GetValue()
{
  return value;
}

//
void TImage::UpdateValue(grf::TImage &V)
{
  BeginPaint();
  InsertPaint(this);

  value = &V;
  UpdateClientWidth (V.Width());
  UpdateClientHeight(V.Height());

  EndPaint();
}

//
void TImage::TComponent_Paint(TPaint &Paint,int NPaintStack)
{
  if (!value) {
    TPanel::TComponent_Paint(Paint,NPaintStack);
    return;
  }
  
  if (value->IsTransparent()) {
    TPanel::TComponent_Paint(Paint,NPaintStack);
    value->TImage_Draw(Paint.DC,Paint.GRect.RThisClient);
  }
  else {
    grf::TRegion OldRegion(Paint.RClientPaint);
    Paint.DC.GetClipPT(OldRegion);

    Paint.DC.SelectClip(Paint.GRect.RThisClient,grf::TDC::ERegionMode_Diff);
    TPanel::TComponent_Paint(Paint,NPaintStack);
  
    Paint.DC.SelectClipPT(OldRegion,grf::TDC::ERegionMode_Copy);
    //Paint.DC.SelectClip(Paint.GRect.RThisClient,grf::TDC::ERegionMode_And);
    value->TImage_Draw(Paint.DC,Paint.GRect.RThisClient);
  }
}

//
void TImage::TComponent_Mouse(TMouse Mouse)
{
  TPanel::TComponent_Mouse(Mouse);
}
